/**
 * Storage封装 封装为一条，cookie 里面为json格式 方便调用
 * 本地存储技术
 */
const STORAGE_KEY = "mall";
export default {
  //存储值
  setItem(key, value, module_name) {
    if (module_name) {
      let val = this.getItem(module_name);
      val[key] = value;
      this.setItem(module_name, val);
    } else {
      let val = this.getStorage();
      val[key] = value;
      window.sessionStorage.setItem(STORAGE_KEY, JSON.stringify(val));
    }
  },

  getItem(key, module_name) {
    // 获取某一个模块下面得属性user下面得userName
    if (module_name) {
      let val = this.getItem(module_name);
      if (val) return val[key];
    }
    return this.getStorage()[key];
  },
  getStorage() {
    return JSON.parse(window.sessionStorage.getItem(STORAGE_KEY) || "{}");
  },
  clear(key, module_name) {
    //清空每一个值
    let val = this.getStorage();
    if (module_name) {
      if(!val[module_name])return
      delete val[module_name][key];
    } else {
      delete val[key];
    }
    window.sessionStorage.setItem(STORAGE_KEY, JSON.stringify(val));
  },
};
